/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cc;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Reader;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.StringTokenizer;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * /
 * /**
 *
 * @author hcc25_000
 */
/*interface Stuff {
 }
 //矿石接口

 interface Ore extends Stuff {
 }
 //武器接口

 interface Weapon extends Stuff {
 }
 //垃圾接口

 interface Rubbish extends Stuff {
 }

 class Gold implements Ore {

 public String toString() {
 return "God";
 }
 }

 class Copper implements Ore {

 public String toString() {
 return "Copper";
 }
 }

 class Gun implements Weapon {

 public String toString() {
 return "Gun";
 }
 }

 class Grenada implements Weapon {

 public String toString() {
 return "Grenada";
 }
 }

 class Stone implements Rubbish {

 public String toString() {
 return "Stone";
 }
 }

 public class Test {

 public static ArrayList<Stuff> collectStuff(Stuff[] s) {
 ArrayList<Stuff> al = new ArrayList<Stuff>();
 for (int i = 0; i < s.length; i++) {
 if (!(s[i] instanceof Rubbish)) {
 al.add(s[i]);
 }
 }
 return al;
 }

 public static void main(String[] args) {
 Stuff[] s = {new Gold(),new Copper(),new Gun(),new Grenada(),new Stone()};
 ArrayList<Stuff> al=collectStuff(s);
 System.out.println("The useful Stuff collected are:");
 for(int i=0;i<al.size();i++)
 System.out.println(al.get(i));
 }
 }*/
/*class obj  implements Cloneable{

 private int aInt = 0;

 public int getAInt() {
 return aInt;
 }

 public void setAInt(int aInt) {
 this.aInt = aInt;
 }

 public void changeInt() {
 this.aInt = 1;
 }

 @Override
 public Object clone() {
 obj o = null;
 try {
 o = (obj)super.clone();
 } catch (CloneNotSupportedException e) {
 e.printStackTrace();
 }
 return o;
 }
 }

 public class Test {

 public static void main(String[] args) {
 obj a = new obj();
 obj b = (obj) a.clone();
 b.changeInt();
 System.out.println("a:" + a.getAInt());
 System.out.println("b:" + b.getAInt());
 }
 }*/
/*class Obj implements Cloneable{
 private Date birth=new Date();
    
 public Date getBirth(){
 return birth;
 }
    
 public void setBirth(Date birth){
 this.birth=birth;
 }
    
 public void changeDate(){
 this.birth.setMonth(4);
 }
    
 public Object clone(){
 Obj o=null;
 try{
 o=(Obj)super.clone();
 }catch(CloneNotSupportedException e){
 }
 o.birth=(Date)this.getBirth().clone();//深复制
 return o;
 }
 }
 public class Test{
 public static void main(String[] args){
 Obj a=new Obj();
 Obj b=(Obj)a.clone();
     
 b.changeDate();
 System.out.println(a.getBirth());
 System.out.println(b.getBirth());
 //System.out.println(b.getClass().getName());
 //new Subject().f();
 try{
 Class c=Class.forName("Subject");
 Subject s=(Subject)c.newInstance();
 s.getInt();
 }catch(Exception e){
            
 }
 }
 }
 class Subject{
 private int o=1;
    
 public int getInt(){
 return o;
 }
 }*/
/*interface IntCompare{
 public int cmp(int a,int b);
 }
 class Cmp1 implements IntCompare{
    
 public int cmp(int a, int b) {
 if(a>b){
 return 1;
 }
 else if(a<b){
 return -1;
 }
 else{
 return 0;
 }
 }
 }

 class Cmp2 implements IntCompare{
    
 public int cmp(int a,int b){
 if(a>b){
 return -1;
 }
 else if(a<b){
 return 1;
 }
 else{
 return 0;
 }
 }
 }
 public class Test{
 public static void insertSort(int a[],IntCompare cmp){
 for(int i=1;i<a.length;i++){
 int temp=a[i];
 int j=i;
 while(j>=1&&(cmp.cmp(a[j-1], temp)==1)){
 a[j]=a[j-1];
 j--;
 }
 a[j]=temp;
 }
 }
    
 public static void main(String[] args){
 int a[]={3,5,1,4};
 IntCompare cmp=new Cmp1();
 insertSort(a,cmp);
 for(int i=0;i<a.length;i++){
 System.out.print(a[i]+"   "); 
 }
        
 System.out.println();
        
 int b[]={3,5,1,4};
 IntCompare cm=new Cmp2();
 insertSort(b,cm);
 for(int i=0;i<b.length;i++){
 System.out.print(b[i]+"   ");
 }
 }
 }*/
/*class Base{
 public Base(){
 g();
 }
    
 public void f(){
 System.out.println("Base f()");
 }
    
 public void g(){
 System.out.println("Base g()");
 }
 }

 class Derived extends Base{
 public void f(){
 System.out.println("Derived f()");
 }
 public void g(){
 System.out.println("Derived g()");
 }
 }
 public class Test{
 public static void main(String[] args){
 Base b=new Derived();
 b.f();
 b.g();
 }
 }*/
/*class Super{
 public int f(){
 return 1;
 }
 }
 class SubClass extends Super{
 public float f(){//编译错误
 return 2f;
 }
 }*/
/*class Base{
 public void f(){
 System.out.println("Base:f()");
 }
 }
 class Sub extends Base{
 @Override
 public void f(){
 System.out.println("Sub:f()");
 }
 public void subf(){
 f();
 }
 public void basef(){
 super.f();
 }
 }
 public class Test{
 public static void main(String[] args){
 Sub s=new Sub();
 s.subf();s.basef();
 }
 }*/
/*class Base{
 public Base(){
 System.out.println("Base");
 }
 public Base(int i){
 System.out.println("Base:"+i);
 }
 }
 class Sub extends Base{
 public Sub(){
 //super(1);
 System.out.println("Sub");
 }
 }
 public class Test{
 public static void main(String[] args){
 //Base s=new Sub();
 }
 }*/
/*public class Test{
 public static void main(String[] args){
 String s1="hello";
 String s2="好";//中文一个字符占三个字节
 getLen(s1);getLen(s2);
 }
    
 public static void getLen(String str){
 System.out.println(str+"的长度:"+str.length()+"  所占字节数："+str.getBytes().length);
 }
 }*/
/*public class Test {

 public static void judgeChineseCharacter(String str) {
 String regEx = "([\u4e00-\u9fa5]);(ello)";

 if (str.length() == str.getBytes().length) {
 System.out.println("无汉字");
 } else {
 Pattern p = Pattern.compile(regEx);
 Matcher m = p.matcher(str);
 while (m.find()) {
 System.out.print(m.group(0) + "");
 }
 }
 }

 public static void main(String[] args) {
 //judgeChineseCharacter("Hello world");
 judgeChineseCharacter("h一ello你好，异议一");
 }
 }*/
/*import java.awt.AWTEventMulticaster;
 import java.awt.Color;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.KeyAdapter;
 import java.awt.event.KeyEvent;
 import java.awt.event.KeyListener;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseListener;
 import javax.swing.JComponent;

 public class Test extends JComponent {

 private ActionListener actionListenerList = null;

 public Test() {
 KeyListener internalKeyListener = new KeyAdapter() {
 public void keyPressed(KeyEvent event) {
 if (actionListenerList != null) {
 int keyCode = event.getKeyCode();//返回事件中的键关联的整数值
 String keyText = event.getKeyText(keyCode);//把与事件关联的整数值转化为字符串
 //System.out.println(keyText);
 ActionEvent actionEvent = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, keyText);
 actionListenerList.actionPerformed(actionEvent);
 }
 }
 };

 MouseListener internalMouseListener = new MouseAdapter() {
 public void mousePressed(MouseEvent event) {
 requestFocusInWindow();
 //setBackground(Color.BLACK);
 }
 };

 addKeyListener(internalKeyListener);
 addMouseListener(internalMouseListener);
 }

 public void addActionListener(ActionListener actionListener) {
 actionListenerList = AWTEventMulticaster.add(actionListenerList, actionListener);
 }

 public void removeActionListener(ActionListener actionListener) {
 actionListenerList = AWTEventMulticaster.remove(actionListenerList, actionListener);
 }

 public boolean isFocusable() {
 return true;
 }
 }*/
public class Test {

    /*public static void test1(){
     System.out.println("test1:begin");
     Thread t1=new ThreadDemo();
     t1.start();
     System.out.println("test1:end");
     }
     public static void test2(){
     System.out.println("test2:begin");
     Thread t1=new ThreadDemo();
     t1.run();
     System.out.println("test2:end");
     }*/
    public static void main(String[] args) {
        //String s="hello cc c1 c2";
        /*StringBuffer sb=new StringBuffer(s);
         sb.append("word");
         System.out.print(sb.toString());*/
        /*StringTokenizer st=new StringTokenizer(s);
         while(st.hasMoreTokens()){
         System.out.println(st.nextToken());
         }*/
        //如何正确读取带有中文字符的文件
        /*int c;
         try {
         InputStream is = new MyOwnInputStream(new BufferedInputStream(new FileInputStream("C:\\Users\\hcc25_000\\Desktop\\readme.txt")));
         while ((c = is.read()) != -1) {
         System.out.print((char) c);
         }
         is.close();
         System.out.println("");
         String s=null;
         BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("C:\\Users\\hcc25_000\\Desktop\\readme.txt"),"UTF-8"));
         while((s=br.readLine())!=null){
         System.out.print(s);
         }
         } catch (FileNotFoundException ex) {
         } catch (IOException ex) {
         }
         }*/
        /*File f=new File("D:\\JavaTestFile");
         System.out.println(new File("D:\\JavaTestFile\\sam.jpg").exists());
         File[] fileList=f.listFiles();
         for(int i=0;i<fileList.length;i++){
         if(fileList[i].isDirectory()){
         System.out.println("this is a dir:"+fileList[i].getName());
         }
         else{
         System.out.println("this is a file:"+fileList[i].getName());
         }
         }*/

        /*People p=new People();
         ObjectOutputStream oos=null;
         ObjectInputStream ois=null;
         try{
         FileOutputStream fos=new FileOutputStream("D:\\JavaTestFile\\test.txt");
         oos=new ObjectOutputStream(fos);
         oos.writeObject(p);
         oos.close();
         }catch(IOException e){
         e.printStackTrace();
         }
         People peo=null;
         try{
         FileInputStream fis=new FileInputStream("D:\\JavaTestFile\\test.txt");
         ois=new ObjectInputStream(fis);
         peo=(People)ois.readObject();
         System.out.println("name:"+peo.getName());
         System.out.println("age:"+peo.getAge());
         ois.close();
         }catch(Exception ex){
         ex.printStackTrace();
         }*/
        /*People p1=new People();
         People p2=p1;
         p1=null;
         System.out.println(p2.getAge());*/
        /*List<HashMap<String,Object>> list=new LinkedList();
         HashMap<String,Object> hm=new HashMap();
         hm.put("c1", 1);
         list.add(hm);
         System.out.println(hm.get("c1"));*/
        /*Iterator i=list.iterator();
         while(i.hasNext()){
         Object s=i.next();
         i.remove();
         } 
         System.out.println(list.size());*/
        /*HashMap<String, Object> hm = new HashMap();
         hm.put("aaa", 111);
         hm.put("aaa", 222);
        
         Iterator iter=hm.entrySet().iterator();

         while(iter.hasNext()){
         Map.Entry entry=(Map.Entry)iter.next();
         System.out.println(entry.getKey());
         System.out.println(entry.getValue());
         }*/
        /*System.out.println("Use String as key:");
         HashMap<Person,String> hm=new HashMap<Person,String>();
         Person p1=new Person("111","name1");
         Person p2=new Person("111","name2");

         hm.put(p1,"address1");hm.put(p2,"address2");
         Iterator iter=hm.entrySet().iterator();
         while(iter.hasNext()){
         Map.Entry entry=(Map.Entry)iter.next();
         Person key=(Person)entry.getKey();
         String val=(String)entry.getValue();
         System.out.println("key="+key+"     value="+val);
         }*/
        /*List<Integer> list=new LinkedList();
         int array[]={1,7,3,2};
         for(int i=0;i<array.length;i++){
         list.add(new Integer(array[i]));
         }
         Collections.sort(list);
         for(int i=0;i<array.length;i++){
         System.out.println(list.get(i));
         }*/
        /*test1();
         try{
         Thread.sleep(5000);
         }catch(InterruptedException ex){
            
         }
         System.out.println();
         test2();*/
        RunDemo rd = new RunDemo();
        Thread t1 = new Thread(rd);
        Thread t2 = new Thread(rd);
        Thread t3 = new Thread(rd);
        t1.start();
        t2.start();
        t3.start();
    }
}

class MyOwnInputStream extends FilterInputStream {

    public MyOwnInputStream(InputStream is) {
        super(is);
    }

    /**
     * 读取文件的字符
     *
     * @return 返回值为变换后打字母;例如大写变小写和小写变大写
     * @throws IOException
     */
    public int read() throws IOException {
        int c = 0;
        if ((c = super.read()) != -1) {
            if (Character.isLowerCase((char) c)) {
                return Character.toUpperCase((char) c);
            } else if (Character.isUpperCase((char) c)) {
                return Character.toLowerCase((char) c);
            } else {
                return c;
            }
        } else {
            return -1;
        }
    }

}

class People implements Serializable {

    private String name;
    private int age;

    public People() {
        this.name = "lili";
        this.age = 20;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    /**
     * 显示信息
     *
     * @param message 传入的参数
     */
    public void showMes(String message) {

    }

}

class Person {

    private String id;
    private String name;

    public Person(String id, String name) {
        this.id = id;
        this.name = name;
    }

    /**
     *
     * 重写toString方法，函数返回对象的私有变量
     *
     * @return 返回函数的私有变量
     */
    public String toString() {
        return "id=" + id + ",name=" + name;
    }

    /**
     * 放回私有变量id
     *
     * @return
     */
    public String getId() {
        return id;
    }

    @Override
    public boolean equals(Object obj) {
        Person p = (Person) obj;
        if (p.getId().equals(this.id)) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public int hashCode() {
        return this.id.hashCode();
    }
}

class ThreadDemo extends Thread {

    @Override
    public void run() {
        System.out.println("TheadDemo:begin");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
        }
        System.out.println("ThreadDemo:end");
    }
}

class RunDemo implements Runnable {

    private int value = 0;

    @Override
    public void run() {
        while (value < 10) {
            try {
                addValue();
                Thread.sleep(50);
            } catch (Exception ex) {

            }
        }
    }

    public synchronized void addValue() {
        if (value == 10) {
            return;
        }
        value++;
        System.out.println(Thread.currentThread().getName() + ";" + value);
    }
}
